package com.lagou.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lagou.mapper.LagouUserMapper;
import com.lagou.model.LagouUser;
import com.lagou.service.ILagouUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author xiaohuzi
 * @since 2022-03-26
 */
@Service
public class LagouUserServiceImpl extends ServiceImpl<LagouUserMapper, LagouUser> implements ILagouUserService {

    @Autowired
    private LagouUserMapper lagouUserMapper;

    /**
     * 检查邮箱是否可用
     *
     * @param email
     * @return
     */
    @Override
    public boolean isEmailRegister(String email) {
        QueryWrapper<LagouUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("email", email);
        List<LagouUser> users = lagouUserMapper.selectList(queryWrapper);
        if (CollectionUtils.isEmpty(users)) {
            return false;
        }
        return true;
    }

    @Override
    public void addUser(String password, String email) {
        LagouUser lagouUser = new LagouUser();
        lagouUser.setCreatetime(new Date());
        lagouUser.setEmail(email);
        lagouUser.setPassword(password);
        lagouUserMapper.insert(lagouUser);
    }

    @Override
    public LagouUser getUserByMailAndPassword(String mail, String password) {
        QueryWrapper queryWrapper = new QueryWrapper<>().eq("email", mail).eq("password", password);
        List list = lagouUserMapper.selectList(queryWrapper);
        return CollectionUtils.isEmpty(list) ? null : (LagouUser) list.get(0);
    }
}
